package com.ys.controller.api.outer;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.sys.SysAdmin;
import com.ys.service.sys.ImgcodeService;
import com.ys.service.sys.SysAdminService;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/outer")
public class ApiOuterLoginController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);

    @Autowired
    private ImgcodeService imgcodeService;
	@Autowired
	private SysAdminService sysAdminService;
	
	@PostMapping("/login")
	@ResponseBody
	public R login(
    		Model model,
			HttpServletRequest request,
			HttpServletResponse response,
			String loginName,
			String pass,
			String code
			) {
		System.out.println("loginname---" + loginName + "，password---" + pass);
		R r = sysAdminService.login(
				request,
				response,
				loginName,
				pass,
				code
				);
		
		if (r.getCode() == R.Code.SUCCESS.value) {
			SysAdmin sysAdmin = new SysAdmin().setLoginName(loginName).queryFirst();
			imgcodeService.doAuthAfterLogin(sysAdmin);
		}
		return r;
	}

	@RequestMapping("/code")
	public void code(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		imgcodeService.getImgCode(request, response);
	}
	
}
